from datetime import datetime

from sqlalchemy import Column, Integer, String, CHAR, DateTime, Float, desc, text, DECIMAL
from app.models.base import db, Base
import time


class sentence(Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    order_number = Column(String(30), nullable=False)
    sentence_index = Column(CHAR(8), nullable=False)
    content = Column(String(150), nullable=False)
    paragraph_index = Column(Integer, nullable=False)
    is_similar = Column(Integer, nullable=False, default=0)
    rate = Column(DECIMAL(5, 2), default=0)
    suggest = Column(String(5000), default='')
    is_author = Column(Integer, default=0)
    check_content = Column(String(300), default='')
    check = Column(Integer, default=0)
    check_id = Column(Integer, default=0)

    def keys(self):
        return ['id', 'order_number', 'sentence_index', 'paragraph_index', 'content', 'is_similar', 'rate', 'suggest',
                'is_author', 'check_content']

    def insert_sentences(self, sentences, order_number):
        try:
            current_check_content = ''
            params = []
            index = 0
            check_index = 0
            now = int(datetime.now().timestamp())
            for item in sentences:
                if item['check'] == 1 and item['check_content'] != current_check_content and item['content'] in item[
                    'check_content']:
                    current_check_content=item['check_content']
                    check_index += 1
                params.append(
                    {
                        'create_time': now,
                        'status': 1,
                        'order_number': order_number,
                        'sentence_index': index,
                        'content': item['content'],
                        'paragraph_index': item['paragraph_index'],
                        'is_similar': 0,
                        'check_content': item['check_content'],
                        'check_id': check_index,
                        'check': item['check']
                    }
                )
                index += 1
            db.session.bulk_insert_mappings(sentence, params)
            db.session.commit()
        except Exception as e:
            print(e)
